package com.hxzy.controller.api.system;

import com.hxzy.common.vo.R;
import com.hxzy.dto.system.DictTypeDTO;
import com.hxzy.entity.system.SysDictType;
import com.hxzy.service.system.SysDictTypeService;
import com.hxzy.vo.system.HxzyUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "字典类型")
@RestController
@RequestMapping(value = "/api")
public class DictTypeController {

    @Autowired
    private SysDictTypeService dictTypeService;

    @ApiOperation(value = "分页查询")
    @PostMapping(value = "/dic/getData")
    public R selectAll(@RequestBody DictTypeDTO dictTypeDTO){
        return R.success(this.dictTypeService.selectAll(dictTypeDTO));
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/dic/findType")
    public R findType(Long id){
        return R.success(this.dictTypeService.findType(id));
    }

    @ApiOperation(value = "新增或者修改字典类型")
    @PostMapping(value = "/dic/addORupdateType")
    public R addType(@RequestBody SysDictType sysDictType){
        // 从redis中取出当前用户信息
        HxzyUser hxzyUser= (HxzyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = hxzyUser.getSysUser().getName();
        if (sysDictType.getId()!=null){
            sysDictType.setUpdateBy(name);
            sysDictType.setUpdateTime(new Date());
            this.dictTypeService.updateById(sysDictType);
        }else {
            sysDictType.setCreateBy(name);
            sysDictType.setCreateTime(new Date());
            this.dictTypeService.save(sysDictType);
        }
        return R.success();
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping(value = "/dic/deleteType")
    public R deleteType(@RequestBody Long[] ids){
        List<Long> list=new ArrayList<>(Arrays.asList(ids));
        return R.success(this.dictTypeService.removeBatchByIds(list));
    }
}
